/**
 * @(#) JobProxy.as
 *
 * @date		2009. 03. 11.
 * @version		1.0
 * @author		Park, JuneHo
 * @description	 계약현황 조회 - Proxy
 * 				 
 */
package com.skt.common_hr.search.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.search.SearchFacade;
	import com.skt.common_hr.search.model.vo.JobVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class JobProxy extends CommonProxy
	{
		public static const NAME:String = "JobProxy";
		
		public var jobList:ArrayCollection;


		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function JobProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load():void
		{
			var param:Object = new Object();
			param["I_OTYPE"] = "C";
			param["I_PLVAR"] = "01";
			//param["I_BEGDA"] = "C";
			//param["I_ENDDA"] = "C";
			param["I_SRPSL"] = "KO";
			loadData("Z_HR_0072", param, result, fault);
		}
		







		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				jobList = parseResult(event.result.OTAB);
				facade.sendNotification(SearchFacade.LOAD_JOB_COMPLETE, this);
			}
			else {
				trace( "failed to load JobProxyLoad: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(SearchFacade.LOAD_JOB_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace("Fault message in JobProxy: " + event.fault.faultString);
			facade.sendNotification(SearchFacade.LOAD_JOB_FAILED, this);
			CursorManager.removeBusyCursor();
		}
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 공지사항 Value Object
		 */
		 private function parseResult(src:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection;
			
			for each(var rec:Object in src)
			{
				var vo:JobVO = new JobVO();
				
				vo.OBJID		= rec.OBJID;		//사번
				vo.STEXT		= rec.STEXT;	//성명
				vo.BEGDA		= DateUtil.stringToDateTime(rec.BEGDA);		//부서명
				vo.ENDDA		= DateUtil.stringToDateTime(rec.ENDDA);		//내용
				list.addItem(vo);
			}
			
			// combo box에  제공하기 위해 첫번째 record를 공백으로 넣어준다.
			vo = new JobVO;
			vo.OBJID		= "";
			vo.STEXT		= "직무선택안함";
			vo.BEGDA		= new Date();
			vo.ENDDA		= new Date();
			list.addItemAt(vo, 0);
			return list;
		}
	}
}